package com.miku.utils;

public class ParseUtils {
	
	public interface Listener{
		String getReplaceString(String id, String name);
	}
	
	/**
	 * 修正之前错误保存的数据格式
	 */
	public static String modifiedErrorFormat(String src){
		if(src == null) return src;
		src = src.replaceAll("<a class=\"linkproduct\" target=\"_blank\" href=\"/web/productpage/","<@ID=");
		src = src.replaceAll("<a class=\"linkproduct\" target=\"_blank\" href=\"/wechat/productpage/","<@ID=");
		src = src.replaceAll("</a>", "@>");
		return src.replaceAll("\">", "，");
	}
	
	/**
	 * 解析带有产品标签的文字，产品标签将会被替换为指向产品链接的地方
	 * <@ID=223060，恩诗红色密码柔 皙舒缓洁面乳@>
	 * @param body 带有产品标签的文字内容
	 * @return
	 */
	public static String parseProductLabel(String body, Listener listener){
		if(body == null) return null;
		
		StringBuilder sb = new StringBuilder();
		int p = body.indexOf("<@ID=");
		int last = 0;
		while(p > -1){
			sb.append(body.substring(last, p));
			
			int ed = body.indexOf("，", p+5);
			if(ed == -1) break;
			
			int ed2 = body.indexOf("@>", ed+1);
			if(ed2 == -1) break;
			String idd = body.substring(p+5, ed);
			String name = body.substring(ed+1, ed2);
			//sb.append("<a class=\"productLink\" target=\"_blank\" href=\"/" + (isWeiXin ? "weixin" : "web") + "/ranklist/d/?p=" + idd + "\">" + name + "</a>");
			sb.append(listener.getReplaceString(idd, name));
			last = ed2 + 2;
			p = body.indexOf("<@ID=", last);
		}
		
		if(last == 0) return body;
		sb.append(body.substring(last, body.length()));
		return sb.toString();
	}
}
